<template>
  <div class="login">
    <div class="login_box">
      <!--  顶部logo区域 -->
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!--  表单区域 -->

      <el-form
        :model="loginForm"
        :rules="rules"
        ref="login_form"
        class="login_form"
      >
        <el-form-item prop="username">
          <i class="el-icon-s-custom"></i>
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <i class="el-icon-unlock"></i>
          <el-input v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //  登录表单的数据
      loginForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        //校验规则的字段和登录表单中的数据字段必须一致  "blur"失去焦点触发
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    login() {
      // console.log(this.loginForm);
      //this.loginForm拿到用户输入的数据
      this.$refs.login_form.validate(async (valid) => {
        // console.log(valid);
        if (!valid) {
          return this.$message.error("账号或密码验证未通过");
        }
        //正确
        //发请求  需要将axios引入到main中，配置全局可用
        const { data } = await this.$axios.post("/login", this.loginForm);
        //异步，不能保证后打印
        // console.log(data.meta);
        if (data.meta.status != 200) {
          return this.$message.error(data.meta.msg);
        }
        //登陆成功  token进行身份认证
        // console.log(data);
        const token = data.data.token;
        //保存token
        localStorage.setItem("token", token);
        // console.log(token);

        this.$message({ message: "登陆成功", type: "success" });

        //编程式跳转页面 this.$router.push()
        this.$router.push("/home");
      });
    },
    reset() {
      this.$refs.login_form.resetFields();
    },
  },
};
</script>

<style scoped   lang="less">
.login {
  /*  百分比相对于父元素说的   */
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    padding: 0 20px;
    .logo {
      position: absolute;
      left: 50%;
      //   transform: translate(-50%, 0);
      margin-left: -76px;
      top: -76px;
      width: 152px;
      height: 152px;
      background-color: #fff;
      padding: 10px;
      border-radius: 50%;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
      img {
        width: 100%;
        height: 100%;
        background-color: #eee;
        border-radius: 50%;
      }
    }
  }

  .login_form {
    position: absolute;
    bottom: 20px;
    width: 410px;
    .btns {
      display: flex;
      justify-content: flex-end;
    }
    i {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 10;
      top: 13px;
      left: 11px;
    }
  }
}
</style>

<style >
.login .el-input__inner {
  padding-left: 30px;
}
</style>